//该页面商品详情
var pro_detail;

//页面加载
$(document).ready(function(){

    //读取登录状态
    var if_login=$.cookie("status");
    //读取用户名
    var username=$.cookie("username");
    console.log(username);
    if(if_login==1){
        $("#login_region").text(username);
        $("#dropdown-login-item").css("display","none");
        $("#dropdown-exit-item").css("display","block");
        
    }else if(if_login==0){
        $("#login_region").text("登录/注册");
        $("#dropdown-login-item").css("display","block");
        $("#dropdown-exit-item").css("display","none");
    }

    productId=$.cookie('productId'); 
    
    var data={
        goodid:productId,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/good/getGood", 
        data:data,  
        async: false,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            console.log(data);
            $("#product-name").text(data.goodName);
            sessionStorage.setItem("productname",data.goodName);
            $(".sub-detail-introduce").text(data.goodInfo);
            $("#dayprice").text(data.dayPrice);
            $("#pro-buy-times").text(data.buyTime);
            $("#monthprice").text(data.monthPrice);
            sessionStorage.setItem("dayprice",data.dayPrice);
            sessionStorage.setItem("monthprice",data.monthPrice);
            $(".totalprice").text(data.goodPrice);
            $("#product-cover").attr("src",data.goodImg1);
            $("#detail-img1").attr("src",data.goodImg1);
            sessionStorage.setItem("productimg",data.goodImg1);
            $("#detail-img2").attr("src",data.goodImg2);
            $("#detail-img3").attr("src",data.goodImg3);
            pro_detail=data;
            //判断是否能够直接购买该商品
            if(pro_detail.buyStatus==true){
                $("#buy-immedia-btn").css("display","inline-block");
            }
            else if(pro_detail.buyStatus==false){
                $("#buy-immedia-btn").css("display","none");
            }

        },
        error: function(xhr, type) {
            console.log("Error");
        }
     });
     
     //初始计算日租总价格
     var dayprice=Number($('#dayprice').text());
     var rent_time_nums=Number( $("#inputsipnner-day").val());
     var newtotalprice=dayprice*rent_time_nums;
     $('#daytotalprice').html(newtotalprice.toFixed(2));

     //判断该商品是否被该用户收藏
     var data2={
        userName:username,
        productId:productId,
    }
     $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/Favour/isFavour", 
        data:data2,  
        async: true,
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            
            //判断是否能够直接购买该商品
            if(data==1){
                $("#add-collect-btn span").text("已收藏");
            }
            else if(data==0){
                $("#add-collect-btn span").text("收藏");;
            }

        },
        error: function(xhr, type) {
            console.log("Error");
        }
     });
 
});

//console.log(pro_detail);//这里为什么是undefined


var rental_type;//租赁类型
var rental_time;//租赁时间
var rental_money//租金

//计算日租总价并传递给参数
function cal_dayrent_func(){
    rental_type=0;
    rental_time=Number($("#inputsipnner-day").val());
    var dayprice=Number($('#dayprice').text());
    rental_money=dayprice*rental_time;
}
//计算月租总价并传递给参数
function cal_monthrent_func(){
    rental_type=1;
    rental_time=Number($("#inputsipnner-month").val());
    var monthprice=Number($('#monthprice').text());
    rental_money=monthprice*rental_time;
}

$("#type-xianzuhoumai-tab").click(function(){
    //是否以租代售 0先租后买  1以租代售
    sessionStorage.setItem("if_installment",0);
})

//设置状态为日租
$("#pills-dayrent-tab").click(function(){
    cal_dayrent_func();
    //是否以租代售 0先租后买  1以租代售
    sessionStorage.setItem("if_installment",0);
})
//设置状态为月租
$("#pills-monthrent-tab").click(function(){
    cal_monthrent_func();
    //是否以租代售 0先租后买  1以租代售
    sessionStorage.setItem("if_installment",0);
})
  //计算日租总价格
$("#inputsipnner-day").on("change", function(event) {
    var dayprice=Number($('#dayprice').text());
    var rent_time_nums=Number( $(this).val());
    var newtotalprice=dayprice*rent_time_nums;
       $('#daytotalprice').html(newtotalprice.toFixed(2));
       cal_dayrent_func();
  });
  //计算月租总价格
$("#inputsipnner-month").on("change", function(event) {
    var monthprice=Number($('#monthprice').text());
    var rent_time_nums=Number( $(this).val());
    var newtotalprice=monthprice*rent_time_nums;
       $('#monthtotalprice').html(newtotalprice.toFixed(2));
       cal_monthrent_func()
  });

//立即租用按钮
$("#pay-button").click(function(){

    if($.cookie("status")==0){
        alert("请先登录！");
        return
    }

    var username=$.cookie("username");
    sessionStorage.setItem("seller",pro_detail.seller);
    sessionStorage.setItem("purchaser",username);
    sessionStorage.setItem("dayOrMonth",rental_type);
    sessionStorage.setItem("productId",pro_detail.goodid);
    sessionStorage.setItem("rental",rental_money);
    sessionStorage.setItem("rentalDay",rental_time);
    sessionStorage.setItem("status",0);
    // console.log( sessionStorage.getItem("seller") );
    window.open("confirmOrder.html");
    
});

//直接购买该商品
$("#buy-immedia-btn").click(function(){

    if($.cookie("status")==0){
        alert("请先登录！");
        return
    }
    sessionStorage.setItem("dayOrMonth",3);
    var username=$.cookie("username");
    sessionStorage.setItem("seller",pro_detail.seller);
    sessionStorage.setItem("purchaser",username);
    sessionStorage.setItem("productId",pro_detail.goodid);
    sessionStorage.setItem("rentalDay",99);
    sessionStorage.setItem("rental",pro_detail.goodPrice);
    sessionStorage.setItem("status",1);
    window.open("confirmOrder.html");
})

//收藏
$("#add-collect-btn").click(function(){

    if($.cookie("status")==0){
        alert("请先登录！");
        return
    }
    console.log("收藏按钮被点击");
    var username=$.cookie("username");
    var data={
        userName:username,
        productId:productId,
    }
    $.ajax({
        type: 'GET',
        url: "http://8.130.103.251:8080/Favour/changeFavour", 
        data: data,  
        ContentType: 'application/json;charset=utf-8',
        success: function(data) {   
            if(data==true){
                $("#add-collect-btn span").text("已收藏");
            }
            else if(data==false){
                $("#add-collect-btn span").text("收藏");
            }
        },
        error: function(xhr, type) {
            console.log("Error");
        }
     });
})


var installment_three;
var installment_six;
var installment_nine;
var installment_twlve;
//以租代售点击事件
$("#type-yizudaishou-tab").click(function(){

     var servicechange=pro_detail.goodPrice*0.01;
     installment_three=pro_detail.goodPrice/3+servicechange;
     installment_six=pro_detail.goodPrice/6+servicechange;
     installment_nine=pro_detail.goodPrice/9+servicechange;
     installment_twlve=pro_detail.goodPrice/12+servicechange;
     $(".rent-money-three").text(installment_three.toFixed(2));
     $(".rent-money-six").text(installment_six.toFixed(2));
     $(".rent-money-nine").text(installment_nine.toFixed(2));
     $(".rent-money-twlve").text(installment_twlve.toFixed(2));
     //是否以租代售 0先租后买  1以租代售
    sessionStorage.setItem("if_installment",1);

});

//以租代售期数选择
$(".choice-item").click(function(){
    var installment_month=$(this).attr("data-mouthnums");
    var installment_price=$(this).find("span").text();
    sessionStorage.setItem("installment_month",installment_month);
    sessionStorage.setItem("installment_price",installment_price);
    //是否以租代售 0先租后买  1以租代售
    sessionStorage.setItem("if_installment",1);
});

//联系卖家
$("#contact-seller-btn").click(function(){
    if($.cookie("status")==0){
        alert("请先登录！");
        return
    }


    var user=$.cookie("username");
    var seller=pro_detail.seller;

    var data ={
        user:user,
        seller:seller,
    }
    $.ajax({
        type: 'POST',
        url: "http://8.130.103.251:8080/conversation/add", 
        data: data,  
        ContentType: 'application/json;charset=utf-8',
        success: function(data) { 
            window.open("message.html");
        },
        error: function(xhr, type) {
            console.log("Error");
        }
    });

});


//导航栏顶部跳转数码专区
$("#digital-area").click(function(){
    $.cookie('pro_type',"数码配件",{ expires: 7 });
    window.open("tags.html");
});

//退出按钮的点击事件
$("#dropdown-exit-item").on("click",function () { 
    $.cookie('username', null);
    $.cookie('status', '0', { expires: 7 });
    window.location.href = "index.html";
    
});